package org.goodsence.frammework.common.exception;

import org.goodsence.frammework.common.utils.ResultUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.util.List;

/**
 * @author zty
 * @apiNote
 * @project_name the-revolt
 * @user tain
 * @create_at 2023/6/14 20:41
 * @create_vio IntelliJ IDEA
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public ResponseEntity<Void> handleConstraintViolationException(MissingServletRequestParameterException e) {
//        log.error(e.getMessage());
        return ResultUtils.error();
    }

    /**
     * 统一处理请求参数校验(实体对象传参)
     * @param e BindException
     * @return IndicatorResponse
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Void> validExceptionHandler(MethodArgumentNotValidException e) {
//        log.error(e.getMessage());
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            message.append(error.getField()).append(error.getDefaultMessage()).append(",");
        }
        message = new StringBuilder(message.substring(0, message.length() - 1));
//        log.error(String.valueOf(message));
        return ResultUtils.error();
    }

    /**
     * 参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public ResponseEntity<Void> bindException(BindException e) {
        FieldError fieldError = e.getFieldError();
        assert fieldError != null;
//        log.error(fieldError.getDefaultMessage());
        return ResultUtils.error();
    }

    /**
     * IO异常
     * @param e
     * @return
     */
    @ExceptionHandler(IOException.class)
    public ResponseEntity<Void> iOException(IOException e) {
        e.printStackTrace();
//        log.error("出现IO异常！, 请拦截异常并关闭IO!");
        return ResultUtils.error();
    }

    /**
     * 获取业务异常
     * @param e 业务异常
     * @return 400
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> businessExceptionHandler(BusinessException e){
//        log.warn(e.getMessage());
        return ResultUtils.badRequest(e.getMessage());
    }


    /**
     * 获取未知异常
     * @param e 未知异常
     * @return  500
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Void> errorHandler(Exception e) {
        e.printStackTrace();
//        log.error(e.getMessage());
        return ResultUtils.error();
    }
}
